import { uploadFile } from "@/api/upload";
import { useAppStore } from "@/store/app";
export const uploadImg = (cb,ccfn = () => {}) => {
  const maxsize = 0;
  const input = document.createElement("input");
  input.type = "file";
  input.accept = "image/*,.jpeg,.png,.jpg";
  input.onchange = function () {
    const img = this.files[0];
    if (img.size > maxsize && maxsize) {
      ElMessage.error("图片大小不能超过4M");
      return;
    }
    const formData = new FormData();
    formData.append("file", img);
    useAppStore().startLoading({
      lock: true,
      text: '上传中...',
      body:true,
      background: 'rgba(255, 255, 255, 0.6)'
    });
    uploadFile(formData)
    .then((res) => {
      useAppStore().closeLoading();
      cb(res)
    })
    .catch((e) => {
      useAppStore().closeLoading();
      ccfn(e)
    });
    input.remove();
  };
  input.click();
};